import { ElMessageBox } from 'element-plus';
import {
	TraShenQingInput, TraShenQingDanInput, TraShenQingDanOutput, TraShenQingApi, TraShenQingYongXueInput,
	TraShenQingHuanZheInput, TraShenQingJianYan
} from '/@/api/tra/TraShenQing';

import { clog, clone, isNull, showError, showSuccess } from '/@/utils';

export const editHuanZheRef = ref();
export const editYongXueRef = ref();
export const listRef = ref();
export const billPanelRef = ref();
export const caoZuoRef = ref();
const initCtx = () => {
	return {
		isEdit: false,
		loading: false,
		pingGuInput: {} as PingGuInput,
		traShenQing: {} as TraShenQingInput,
		traShenQingHuanZhe: {} as TraShenQingHuanZheInput,
		traShenQingYongXue: {} as TraShenQingYongXueInput,
		traShenQingJianYanList: [] as TraShenQingJianYan[],
	};
};

export const ctx = reactive(initCtx());

export const newEntity = () => {
	Object.assign(ctx, initCtx());
	ctx.isEdit = true;
};

export const loadEntity = async (row: any) => {
	newEntity();
	ctx.isEdit = false;
	let billInfo = await TraShenQingApi.get(row.id);
	ctx.traShenQing = billInfo.traShenQing;
	ctx.traShenQingHuanZhe = billInfo.traShenQingHuanZhe;
	ctx.traShenQingYongXue = billInfo.traShenQingYongXue;
	ctx.traShenQingJianYanList = billInfo.traShenQingJianYanList
};

/** 提交前的校验 */
const beforeSubmit = async () => {
	if (isNull(ctx.traShenQingHuanZhe.huanZheId)) {
		showError('请回车带出患者信息')
		editHuanZheRef.value.patientInpFocus()
		return false
	}
	if (ctx.traShenQingHuanZhe.tongYiShu != '是') {
		let isContinue = false
		await ElMessageBox.confirm('患者是否已签署知情同意书？', '注意', {
			type: 'warning'
		}).then(() => {
			ctx.traShenQingHuanZhe.tongYiShu = '是'
			isContinue = true
		}).catch(() => {
			isContinue = false
		})
		if (!isContinue) return false
	}
	const huanZheValidate = await editHuanZheRef.value.validate()
	if (!huanZheValidate) {
		return false
	}
	const yongXueValidate = await editYongXueRef.value.validate()
	if (!yongXueValidate) {
		return false
	}
	return true
}

export const submit = async () => {
	// clog('traShenQing', ctx.traShenQing);
	// clog('shenQingYongXue', ctx.traShenQingHuanZhe);
	// clog('shenQingHuanZhe', ctx.traShenQingYongXue);
	// clog('JianYan', ctx.traShenQingJianYanList);

	if (!await beforeSubmit()) return

	var input = {
		traShenQing: ctx.traShenQing,
		traShenQingHuanZhe: ctx.traShenQingHuanZhe,
		traShenQingYongXue: ctx.traShenQingYongXue,
		traShenQingJianYanList: ctx.traShenQingJianYanList,
	} as TraShenQingDanInput

	if (isNull(ctx.traShenQing.id)) {
		ctx.traShenQing = await TraShenQingApi.add(input);
	} else {
		await TraShenQingApi.update(input);
	}

	ctx.isEdit = false;
	showSuccess();
};

// 修改单据
export const edit = () => {
	ctx.isEdit = true;
};

export const cancel = () => { };

export const print = () => { };

export const printTongYiShu = () => { };

export const shangJiShenHe = (info) => {
	if (!info) return
	const { shenHeRenId, shenHeXinXi, shenHeYiJian, shenHeRen } = info;
	TraShenQingApi.shangJiYiShengShenHe({
		id: ctx.traShenQing.id,
		shenHeRenId,
		shenHeXinXi,
		shenHeRen
	}).then(re => {
		showSuccess('上级审核成功')
		loadEntity({ id: ctx.traShenQing.id })
	})
	// ctx.traShenQing.shangJiShenHeYiJian = shenHeYiJian;
}
export const shuXueKeShenHe = (info) => {
	if (!info) return
	const { shenHeRenId, shenHeXinXi, shenHeYiJian, shenHeRen } = info;
	TraShenQingApi.shuXueKeShenHe({
		id: ctx.traShenQing.id,
		shenHeRenId,
		shenHeXinXi,
		shenHeRen
	}).then(re => {
		showSuccess('输血科审核成功')
		loadEntity({ id: ctx.traShenQing.id })
	})
}